package com.hui.controller;

import com.github.pagehelper.PageInfo;
import com.hui.entity.PromotionAd;
import com.hui.entity.PromotionAdVo;
import com.hui.entity.ResponseResult;
import com.hui.service.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/08/06/17:38
 * @Description:
 */
@RestController
@RequestMapping("/PromotionAd")
public class PromotionAdController {
    @Autowired
    private PromotionAdService promotionAdService;

    // 分页获取广告列表数据
    @RequestMapping("/findAllPromotionAdByPage")
    private ResponseResult findAllPromotionAdByPage(PromotionAdVo promotionAdVo) {
        PageInfo<PromotionAd> pageInfo = promotionAdService.getAllPromotionAd(promotionAdVo);
        return new ResponseResult(true, 200, "分页查询成功", pageInfo);
    }

    // 图片上传
    @RequestMapping(value = "/PromotionAdUpload", method = RequestMethod.POST)
    public ResponseResult courseUpload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        if (file != null) {
            String originalFilename = file.getOriginalFilename();
            // 根据时间戳构建新的文件名
            String newFileName = System.currentTimeMillis() + originalFilename;
            // 获取当前路径 "C:\apache-tomcat-8.5.55\webapps\ssm-web"
            String realPath = request.getServletContext().getRealPath("/");
            // 去掉后面的项目名  "C:\apache-tomcat-8.5.55\webapps\"
            String path = realPath.substring(0, realPath.indexOf("ssm-web"));
            // 文件上传
            file.transferTo(new File(path + "\\upload\\" + newFileName));

            // 将文件相关信息返回出去
            Map<String, String> map = new HashMap<>();
            map.put("fileName", newFileName);// 文件名
            map.put("filePath", "http://localhost:8080/upload/" + newFileName); // 直接访问路径
            return new ResponseResult(true, 200, "响应成功", map);
        }
        return null;
    }

    // 新增&修改广告
    @RequestMapping("/saveOrUpdatePromotionAd")
    public ResponseResult saveOrUpdatePromotionAd(@RequestBody PromotionAd promotionAd){
        if(promotionAd.getId()==null){
            promotionAdService.savePromotionAd(promotionAd);
            return new ResponseResult(true,200,"添加成功",null);
        }else{
            promotionAdService.updatePromotionAd(promotionAd);
            return new ResponseResult(true,200,"修改成功",null);
        }
    }

    // 回显广告信息
    @RequestMapping("/findPromotionAdById")
    public ResponseResult findPromotionAdById(int id){
        PromotionAd promotionAd = promotionAdService.getPromotionAdById(id);
        return new ResponseResult(true,200,"查询成功",promotionAd);
    }

    // 修改广告状态上下线
    @RequestMapping("/updatePromotionAdStatus")
    public ResponseResult updatePromotionAdStatus(int id ,int status){
        promotionAdService.updateStatus(id,status);
        Map<String,Integer> map = new HashMap<>();
        map.put("status",status);
        return new ResponseResult(true,200,"更新成功",map);
    }
}
